package com.genius.reservation.user.service.impl;

import cn.hutool.core.net.URLDecoder;
import cn.hutool.core.net.URLEncodeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genius.reservation.common.ErrorCode;
import com.genius.reservation.common.GlobalExceptionHandler.GlobalExceptionHandler;
import com.genius.reservation.common.ResponseRule;
import com.genius.reservation.common.constant.Constant;
import com.genius.reservation.driver.entity.DriverInformation;
import com.genius.reservation.driver.service.IDriverInformationService;
import com.genius.reservation.guardPerson.entity.GuardPersonInformation;
import com.genius.reservation.guardPerson.service.IGuardPersonInformationService;
import com.genius.reservation.mapper.UserInformationMapper;
import com.genius.reservation.receiveManager.entity.ReceiveManagerInformation;
import com.genius.reservation.receiveManager.service.IReceiveManagerInformationService;
import com.genius.reservation.receiver.entity.ReceiverInformation;
import com.genius.reservation.receiver.service.IReceiverInformationService;
import com.genius.reservation.reservation.service.IReservationInformationService;
import com.genius.reservation.supplier.entity.SupplierInformation;
import com.genius.reservation.supplier.service.ISupplierService;
import com.genius.reservation.tools.annotation.webLog;
import com.genius.reservation.user.entity.UserInformation;
import com.genius.reservation.user.service.IUserInformationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.genius.reservation.common.constant.RoleType;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.logging.ConsoleHandler;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2022-05-28
 */
@Service
public class UserInformationServiceImpl extends ServiceImpl<UserInformationMapper, UserInformation> implements IUserInformationService {
    private Logger logger = LoggerFactory.getLogger(UserInformationServiceImpl.class);

    @Autowired
    UserInformationMapper usermapper;

    @Autowired
    IDriverInformationService driverService;

    @Autowired
    IReceiverInformationService receiverService;

    @Autowired
    IGuardPersonInformationService guarderService;

    @Autowired
    IReceiveManagerInformationService receiveManagerService;

    @Autowired
    ISupplierService supplierService;


    @Override
    public UserInformation getUserByUserNumber(String userNumber) {
        LambdaQueryWrapper<UserInformation> eq = usermapper.getQueryWrapper().eq(UserInformation::getUserNumber, userNumber);
        return usermapper.selectOne(eq);
    }

    @Override
    public JSONObject updateUserByUserNumber(String userNumber) {
        return null;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseRule registerUser(UserInformation user) {
            switch (user.getUserRoleType()){
                case RoleType.DRIVER:
                    DriverInformation driverInformation = driverService.newDriver(user);
                    user.setNickName(URLEncodeUtil.encode(user.getNickName()));
                    user.setUserNumber(driverInformation.getDriverNumber());
                    UserInformation u = this.createUser(user);
                    return ResponseRule.ok(u,"注册成功");
                case RoleType.RECEIVER:
                    ReceiverInformation receiver = receiverService.createReceiver(user);
                    user.setNickName(URLEncodeUtil.encode(user.getNickName()));
                    user.setUserNumber(receiver.getReceiverNumber());
                    UserInformation u1 = this.createUser(user);
                    return ResponseRule.ok(u1,"注册成功");
                case RoleType.GUARDER:
                    GuardPersonInformation guarder = guarderService.createGuarder(user);
                    user.setNickName(URLEncodeUtil.encode(user.getNickName()));
                    user.setUserNumber(guarder.getGuardPersonNumber());
                    UserInformation u2 = this.createUser(user);
                    return ResponseRule.ok(u2,"注册成功");
                case RoleType.RECEIVE_MANAGER:
                    ReceiveManagerInformation receiveManager = receiveManagerService.createReceiveManager(user);
                    user.setNickName(URLEncodeUtil.encode(user.getNickName()));
                    user.setUserNumber(receiveManager.getReceiveManagerNumber());
                    UserInformation u3 = this.createUser(user);
                    return ResponseRule.ok(u3,"注册成功");
                case RoleType.SUPPLIER:
                    SupplierInformation supplier = supplierService.createSupplier(user);
                    user.setNickName(URLEncodeUtil.encode(user.getNickName()));
                    user.setUserNumber(supplier.getSupplierNumber());
                    UserInformation u4 = this.createUser(user);
                    return ResponseRule.ok(u4,"注册成功");
                default:
                    logger.error("注册错误找不到注册类型");
                    return ResponseRule.no("注册失败");
            }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseRule registerUser(String userName,String userPassword,String wxOpenId,String nickName,String avatar,String phone) {
        LambdaQueryWrapper<UserInformation> eq = usermapper.getWrapper().
                eq(UserInformation::getUserName, userName).
                eq(UserInformation::getPassword, userPassword).
                eq(UserInformation::getStatus,0);
        UserInformation user = usermapper.selectOne(eq);
        if (ObjectUtil.isEmpty(user)) {
            return ResponseRule.no("未找到用户");
        }
        switch (user.getUserRoleType()){
//            case RoleType.DRIVER:
//                DriverInformation driverInformation = driverService.newDriver(user);
//                user.setNickName(URLEncodeUtil.encode(user.getNickName()));
//                user.setUserNumber(driverInformation.getDriverNumber());
//                user.setAvatarUrl(avatar);
//                user.setNickName(nickName);
//                user.setPhone(phone);
//                user.setWxNumber(wxOpenId);
//                UserInformation u = this.addAndUp(user);
//                return ResponseRule.ok(this.addAndUp(user),"登录成功");
            case RoleType.RECEIVER:
                ReceiverInformation receiver = receiverService.createReceiver(user);
                user.setNickName(URLEncodeUtil.encode(user.getNickName()));
                user.setUserNumber(receiver.getReceiverNumber());
                user.setAvatarUrl(avatar);
                user.setNickName(nickName);
//                user.setPhone(phone);
                user.setWxNumber(wxOpenId);
                return ResponseRule.ok(this.addAndUp(user),"登录成功");
            case RoleType.GUARDER:
                GuardPersonInformation guarder = guarderService.createGuarder(user);
                user.setUserNumber(guarder.getGuardPersonNumber());
                user.setAvatarUrl(avatar);
                user.setNickName(nickName);
//                user.setPhone(phone);
                user.setWxNumber(wxOpenId);
                return ResponseRule.ok(this.addAndUp(user),"登录成功");
            case RoleType.RECEIVE_MANAGER:
                ReceiveManagerInformation receiveManager = receiveManagerService.createReceiveManager(user);
                user.setUserNumber(receiveManager.getReceiveManagerNumber());
                user.setAvatarUrl(avatar);
                user.setNickName(nickName);
//                user.setPhone(phone);
                user.setWxNumber(wxOpenId);
                return ResponseRule.ok(this.addAndUp(user),"登录成功");
            case RoleType.SUPPLIER:
                SupplierInformation supplier = supplierService.createSupplier(user);
                user.setUserNumber(supplier.getSupplierNumber());
                user.setAvatarUrl(avatar);
                user.setNickName(nickName);
//                user.setPhone(phone);
                user.setWxNumber(wxOpenId);
                return ResponseRule.ok(this.addAndUp(user),"登录成功");
            default:
                logger.error("注册错误找不到注册类型");
                return ResponseRule.no("注册失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @webLog
    public ResponseRule editUser(UserInformation user) {
        LambdaQueryWrapper<UserInformation> wrapper = this.getBaseMapper().getWrapper().eq(UserInformation::getUserNumber, user.getUserNumber());
        UserInformation one = usermapper.selectOne(wrapper);
        one.setUserRoleType(user.getUserRoleType());
        one.setName(user.getName());
        boolean b = this.saveOrUpdate(one);
        if (b) return ResponseRule.ok(one,"修改成功");
        return ResponseRule.no(null,"修改失败");
    }

    @Override
    public ResponseRule loginUser(String wxNumber) {
        System.out.println(wxNumber);
        LambdaQueryWrapper<UserInformation> eq = usermapper.getWrapper().eq(UserInformation::getWxNumber, wxNumber);
        UserInformation one = usermapper.selectOne(eq);
        if (ObjectUtil.isEmpty(one)){
           return ResponseRule.no("登录失败,没有找到用户");
        }
        one.setNickName(new String(URLDecoder.decode(one.getNickName().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8));
        return ResponseRule.ok(one,"登录成功");
    }

    @Override
    public ResponseRule modifyPassword(String oldpassword, String password, String tpassword,String userNumber) {
        LambdaQueryWrapper<UserInformation> eq = usermapper.getWrapper().eq(UserInformation::getUserName, userNumber).eq(UserInformation::getPassword,oldpassword);
        UserInformation one = usermapper.selectOne(eq);
        if(ObjectUtil.isEmpty(one)) return ResponseRule.no("旧密码填写错误");
        one.setPassword(password);
        usermapper.update(one);
        return ResponseRule.ok("修改密码成功！");
    }

    @Override
    public UserInformation login2(String userNumber,String userPassword) {
        LambdaQueryWrapper<UserInformation> eq = usermapper.getWrapper().
                eq(UserInformation::getUserName, userNumber).
                eq(UserInformation::getPassword, userPassword).
                eq(UserInformation::getStatus,0);
        return usermapper.selectOne(eq);
    }


}
